package br.com.stefanini.schs.ejb.sessionBean;

import java.io.Serializable;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import br.com.stefanini.schs.ejb.entity.Funcionario;
import br.com.stefanini.schs.ejb.entity.Justificativa;

@Stateless
public class JustificativaSessionBean implements Serializable {

	private static final long serialVersionUID = 1L;
	
	private Funcionario funcionario;
	
	@PersistenceContext
	private EntityManager entityManager;
	
	public void enviarJustificativa(Justificativa justificativa){
		funcionario = entityManager.find(Funcionario.class, justificativa.getFuncionario().getIdFuncionario());
		justificativa.setFuncionario(funcionario);
		justificativa.setIdJustificativa(0);
		this.entityManager.persist(justificativa);
	}
	
	public void atualizarJustificativa(Justificativa justificativa){
		this.entityManager.merge(justificativa);
	}
	
	public void excluirJustificativa(Justificativa justificativa) {
		justificativa = entityManager.find(Justificativa.class, justificativa.getIdJustificativa());
		this.entityManager.remove(justificativa);
	}
	
	@SuppressWarnings("unchecked")
	public List<Justificativa> listarTodasJustificativas(){
		Query query = entityManager.createNamedQuery("Justificativa.consultarTodasJustificativas");
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Justificativa> baixarListaJustificativasEmAnalise(Long idFuncionario, int status) {
		Query query = entityManager.createNamedQuery("Justificativa.consultarJustificativaFuncionario");
		query.setParameter("idFuncionario", idFuncionario);
		query.setParameter("status", status);
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}
	@SuppressWarnings("unchecked")
	public List<Justificativa> consultarJustificativasEmAnalise() {
		Query query = entityManager.createNamedQuery("Justificativa.consultarJustificativasEmAnalise");
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}
	@SuppressWarnings("unchecked")
	public List<Justificativa> consultarJustificativasEmAnaliseMes(Long mes) {
		Query query = entityManager.createQuery("select j from Justificativa j where j.documento.status = 1 and MONTH(j.dataJustificada) = :mes");
		query.setParameter("mes", Integer.parseInt(String.valueOf(mes)));
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}
	@SuppressWarnings("unchecked")
	public List<Justificativa> consultarJustificativasEmAnaliseAno(Long ano) {
		Query query = entityManager.createQuery("select j from Justificativa j where j.documento.status = 1 and year(j.dataJustificada) = :ano");
		query.setParameter("ano", Integer.parseInt(String.valueOf(ano)));
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}
	@SuppressWarnings("unchecked")
	public List<Justificativa> consultarJustificativasEmAnaliseMesAno(Long mes, Long ano) {
		Query query = entityManager.createQuery("select j from Justificativa j where j.documento.status = 1 and MONTH(j.dataJustificada) = :mes and year(j.dataJustificada) = :ano");
		query.setParameter("mes", Integer.parseInt(String.valueOf(mes)));
		query.setParameter("ano", Integer.parseInt(String.valueOf(ano)));
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}
	@SuppressWarnings("unchecked")
	public List<Justificativa> consultarJustificativasAceitaMes(Long mes) {
		Query query = entityManager.createQuery("select j from Justificativa j where j.documento.status = 2 and MONTH(j.dataJustificada) = :mes");
		query.setParameter("mes", Integer.parseInt(String.valueOf(mes)));
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}
	@SuppressWarnings("unchecked")
	public List<Justificativa> consultarJustificativasAceitaAno(Long ano) {
		Query query = entityManager.createQuery("select j from Justificativa j where j.documento.status = 2 and year(j.dataJustificada) = :ano");
		query.setParameter("ano", Integer.parseInt(String.valueOf(ano)));
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}
	@SuppressWarnings("unchecked")
	public List<Justificativa> consultarJustificativasAceitaMesAno(Long mes, Long ano) {
		Query query = entityManager.createQuery("select j from Justificativa j where j.documento.status = 2 and MONTH(j.dataJustificada) = :mes and year(j.dataJustificada) = :ano");
		query.setParameter("mes", Integer.parseInt(String.valueOf(mes)));
		query.setParameter("ano", Integer.parseInt(String.valueOf(ano)));
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}
	@SuppressWarnings("unchecked")
	public List<Justificativa> consultarJustificativasRejeitadaMes(Long mes) {
		Query query = entityManager.createQuery("select j from Justificativa j where j.documento.status = 3 and MONTH(j.dataJustificada) = :mes");
		query.setParameter("mes", Integer.parseInt(String.valueOf(mes)));
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}
	@SuppressWarnings("unchecked")
	public List<Justificativa> consultarJustificativasRejeitadaAno(Long ano) {
		Query query = entityManager.createQuery("select j from Justificativa j where j.documento.status = 3 and year(j.dataJustificada) = :ano");
		query.setParameter("ano", Integer.parseInt(String.valueOf(ano)));
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}
	@SuppressWarnings("unchecked")
	public List<Justificativa> consultarJustificativasRejeitadaMesAno(Long mes, Long ano) {
		Query query = entityManager.createQuery("select j from Justificativa j where j.documento.status = 3 and MONTH(j.dataJustificada) = :mes and year(j.dataJustificada) = :ano");
		query.setParameter("mes", Integer.parseInt(String.valueOf(mes)));
		query.setParameter("ano", Integer.parseInt(String.valueOf(ano)));
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}
	@SuppressWarnings("unchecked")
	public List<Justificativa> consultarJustificativasTodasMes(Long mes) {
		Query query = entityManager.createQuery("select j from Justificativa j where MONTH(j.dataJustificada) = :mes");
		query.setParameter("mes", Integer.parseInt(String.valueOf(mes)));
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}
	@SuppressWarnings("unchecked")
	public List<Justificativa> consultarJustificativasTodasAno(Long ano) {
		Query query = entityManager.createQuery("select j from Justificativa j where year(j.dataJustificada) = :ano");
		query.setParameter("ano", Integer.parseInt(String.valueOf(ano)));
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}
	@SuppressWarnings("unchecked")
	public List<Justificativa> consultarJustificativasTodasMesAno(Long mes, Long ano) {
		Query query = entityManager.createQuery("select j from Justificativa j where MONTH(j.dataJustificada) = :mes and year(j.dataJustificada) = :ano");
		query.setParameter("mes", Integer.parseInt(String.valueOf(mes)));
		query.setParameter("ano", Integer.parseInt(String.valueOf(ano)));
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Justificativa> consultarJustificativasAceitas() {
		Query query = entityManager.createNamedQuery("Justificativa.consultarJustificativasAceitas");
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}
	@SuppressWarnings("unchecked")
	public List<Justificativa> consultarJustificativasRejeitadas() {
		Query query = entityManager.createNamedQuery("Justificativa.consultarJustificativasRejeitadas");
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}
	
	public Justificativa consultarJustificativaPorData(Long dia, Long mes, Long ano){
		Query query = entityManager.createQuery("select j from Justificativa j where day(j.dataJustificada) = :dia and MONTH(j.dataJustificada) = :mes and year(j.dataJustificada) = :ano");
		query.setParameter("dia", Integer.parseInt(String.valueOf(dia)));
		query.setParameter("mes", Integer.parseInt(String.valueOf(mes)));
		query.setParameter("ano", Integer.parseInt(String.valueOf(ano)));
		try {
			return (Justificativa) query.getSingleResult();
		} catch (NoResultException e) {
			return null;
		}
	}
	

}
